/**
 * @Project: YonYouWeChat2
 * @Title: MNews.java
 * @Package org.wechat.core.constants.util
 * @author wenxu
 * @date 2015-6-2 下午02:49:18
 * @Copyright: 2015
 * @version V1.0
 */

package com.jeesite.modules.sys.wx.qy.util;


import com.github.sd4324530.fastweixin.company.api.config.QYAPIConfig;
import net.sf.json.JSONObject;

/**
 * @ClassName: MNews
 * @Description: 消息类型及数据格式
 * @author wenxu
 * @date 2015-6-2 下午02:49:18
 *
 */

public class MNews {

    public static String MESSGES = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";


    public static String news(String touser, String toparty, String totag, String title, String description, String url, int agentid) {
        String postData = "{\"touser\": \"%s\",\"toparty\": \"%s\",\"totag\": \"%s\",\"msgtype\": \"news\",\"agentid\": \"%s\",\"news\": {\"articles\":[{\"title\": \"%s\",\"description\": \"%s\",\"url\": \"%s\"}]}}";
        postData = String.format(postData, touser, toparty, totag, agentid, title, description, url);
        return postData;
    }

    public static String sendMessages(String touser, String toparty, String totag, String title, String description, String url, int agentid) {

        QYAPIConfig apiConfig = new QYAPIConfig(QyConfigUtil.corpId,QyConfigUtil.corpSecret);
        String accessToken = apiConfig.getAccessToken();
        String str = MNews.news(touser,toparty,totag,title, description,url,agentid);
        MESSGES = MESSGES.replace("ACCESS_TOKEN", accessToken);
        JSONObject jsonObject = WeixinUtil.HttpRequest(MESSGES, "POST", str);
        //请求成功
        if(null == jsonObject){
            jsonObject = new JSONObject();
        }
        return jsonObject.toString();
    }

    public static void main(String[] args) {
        QYAPIConfig apiConfig = new QYAPIConfig("", "");
        String access_token =  apiConfig.getAccessToken();
        String str = MNews.news("13123366761","@all","@all", "测试图文消息", "您有一条短消息","www.qq.com",1000039);
        MESSGES = MESSGES.replace("ACCESS_TOKEN", access_token);
        JSONObject jsonObject = WeixinUtil.HttpRequest(MESSGES, "POST", str);
        //请求成功
        if(null != jsonObject){
            System.out.println(jsonObject.toString());
        }
    }
}
